package com.thundersoft.pickcolor.viewholder;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.thundersoft.pickcolor.R;
import com.thundersoft.pickcolor.activity.CollectActivity;
import com.thundersoft.pickcolor.bean.Color;
import com.thundersoft.pickcolor.utils.ShareUtils;
import com.thundersoft.pickcolor.utils.TextUtils;
import com.thundersoft.pickcolor.utils.ToastUtils;

public class TypeTwoViewHolder extends TypeAbstractViewHolder{
    Context mContext;
    ImageView iv_color;
    TextView tv_color_value;
    public TypeTwoViewHolder(View itemView) {
        super(itemView);
        mContext = itemView.getContext();
        iv_color = itemView.findViewById(R.id.iv_color);
        tv_color_value = itemView.findViewById(R.id.tv_color_value);
    }

    @Override
    public void bindHolder(final Color color){
        iv_color.setBackgroundColor(android.graphics.Color.parseColor(color.getHexInt()));
        tv_color_value.setTextColor(mContext.getResources().getColor(android.R.color.black));
        tv_color_value.setText((color.getDescribe()==null || color.getDescribe().equals("")?"":
                color.getDescribe()+"\n")+color.getHexInt()+
                "\n"+color.getrInt()+","+color.getgInt()+","+color.getbInt());

        iv_color.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mContext.getClass().equals(new CollectActivity().getClass())){
                    ToastUtils.Info(mContext,mContext.getString(R.string.long_click_cancel_collect_color));
                }else {
                    ToastUtils.Info(mContext,mContext.getString(R.string.long_click_collect_color));
                }
            }
        });

        iv_color.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(mContext.getClass().equals(new CollectActivity().getClass())){
                    // 取消收藏
                    if(deleteData(color)){
                        ToastUtils.Success(mContext,mContext.getString(R.string.cancel_collect_success));
                    }else {
                        ToastUtils.Success(mContext,mContext.getString(R.string.cancel_collect_failed));
                    }
                }else {
                    // 收藏
                    if(queryData(color)){
                        insertData(color);
                        ToastUtils.Success(mContext,mContext.getString(R.string.collect_success));
                    }else {
                        ToastUtils.Success(mContext,mContext.getString(R.string.has_been_collected));
                    }
                }
                return true;
            }
        });

        tv_color_value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.Info(mContext,mContext.getString(R.string.long_click_copy_color_values));
            }
        });
        tv_color_value.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // 复制
                String colorValue = tv_color_value.getText().toString().trim();
                copyColorValue(colorValue);
                ToastUtils.Success(mContext,mContext.getString(R.string.copy_color_value_success));
                return true;
            }
        });
    }

    private boolean deleteData(Color color) {
        // 删除数据
        Uri uri = Uri.parse("content://com.thundersoft.pickcolor.provider/color/"+color.getId());
        mContext.getContentResolver().delete(uri,null,null);
        return true;
    }

    private boolean queryData(Color color) {
        // 查询数据
        Uri uri = Uri.parse("content://com.thundersoft.pickcolor.provider/color");
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(uri,null,
                    "kind = ? and hexValue = ? and userId = ?",
                    new String[]{mContext.getString(R.string.collect), color.getHexInt(),
                            String.valueOf(ShareUtils.getInt(mContext,"userId",0))},
                    null);
            if(cursor !=null){
                while (cursor.moveToNext()){
                    return false;// 已被收藏过了（当前用户）
                }
                return true;
            }else {
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(cursor !=null){
                cursor.close();
            }
        }
        return false;
    }

    private void insertData(Color color) {
        // 添加数据
        Uri uriColor = Uri.parse("content://com.thundersoft.pickcolor.provider/color");
        ContentValues values = new ContentValues();
        values.put("hexValue",color.getHexInt());
        values.put("rValue",color.getrInt());
        values.put("gValue",color.getgInt());
        values.put("bValue",color.getbInt());
        values.put("kind",mContext.getString(R.string.collect));
        values.put("describe",color.getDescribe());
        values.put("userId",ShareUtils.getInt(mContext,"userId",0));
        mContext.getContentResolver().insert(uriColor,values);
    }

    private void copyColorValue(String colorValue) {
        TextUtils.copy(mContext,colorValue);
    }
}
